<template>
  <div>
    <!-- 使用isAdd 展示添加还是编辑 -->
    <el-dialog :title="info.isAdd?'规格添加':'规格编辑'" :visible.sync="info.isshow" @closed="closed">
      {{form}}--{{arr}}
      <el-form :model="form">
        <!-- 2.绑定数据 -->

        <el-form-item label="规格名称" label-width="120px">
          <el-input v-model="form.specsname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="规格属性" label-width="120px" v-for="(item,index) in arr" :key="index">
          <div class="box">
            <el-input v-model="item.value" autocomplete="off"></el-input>
            <el-button type="primary" v-if="index==0" @click="addArr">新增规格属性</el-button>
            <el-button type="danger" v-else @click="delArr(index)">删除</el-button>
          </div>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add">添 加</el-button>
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  reqspecsadd,
  reqspecsedit,
  reqspecsinfo,
} from "../../../request/api";
import { successAlert } from "../../../utils/alert";

import { mapActions } from "vuex";
export default {
  components: {},
  props: ["info"],
  data() {
    return {
      // 1.初始化数据
      form: {
        specsname: "",
        attrs: "",
        status: 2,
      },
      //规格数组
      arr: [
        {
          value: "",
        },
      ],
    };
  },
 
  mounted() {},
  methods: {
    ...mapActions({
      reqList: "specs/reqList",
      //请总数
      reqCount: "specs/reqCount",
    }),

    //新增规格属性
    addArr() {
      this.arr.push({
        value: "",
      });
    },
    //删除规格属性
    delArr(index) {
      this.arr.splice(index, 1);
    },

    //取消
    cancel() {
      this.info.isshow = false;
    },
    //弹框关闭
    closed() {
      if (!this.info.isAdd) {
        this.empty();
      }
    },
    //重置form
    empty() {
      // 1.初始化数据
      this.form = {
        specsname: "",
        attrs: "",
        status: 2,
      };
      //规格数组
      this.arr = [
        {
          value: "",
        },
      ];
    },
    //添加
    add() {
      // arr=[ { "value": "s" }, { "value": "m" }, { "value": "l" }, { "value": "xl" } ]----["s","m","l","xl"]->"s,m,l,xl"
      this.form.attrs = this.arr.map((item) => item.value).join(",");
      // 数据交互
      reqspecsadd(this.form).then((res) => {
        if (res.data.code == 200) {
          //成功了
          // （1）弹成功
          successAlert(res.data.msg);
          // (2)弹框消失
          this.cancel();
          // (3)清空数据
          this.empty();
          // (4)刷新列表
          this.reqList();
          this.reqCount()
        }
      });
    },
    //获取一条数据
    getOne(id) {
      reqspecsinfo({ id: id }).then((res) => {
        if (res.data.code == 200) {
          this.form = res.data.list[0];
          // "attrs": [ "S", "M", "L" ] --> [{value:"S"},{value:"M"},{value:"L"}]
          this.arr = this.form.attrs.map((item) => ({ value: item }));
        }
      });
    },
    //修改
    update() {
         // arr=[ { "value": "s" }, { "value": "m" }, { "value": "l" }, { "value": "xl" } ]----["s","m","l","xl"]->"s,m,l,xl"
      this.form.attrs = this.arr.map((item) => item.value).join(",");
      reqspecsedit(this.form).then((res) => {
        if (res.data.code == 200) {
          // （1）弹成功
          successAlert(res.data.msg);
          // (2)弹框消失
          this.cancel();
          // (3)清空数据
          this.empty();
          // (4)刷新列表
          this.reqList();
        }
      });
    },
  },
};
</script>
<style lang="less" scoped>
@import "../../../less/index.less";
.box {
  display: flex;
}
.box .el-input {
  flex: 1;
}
.box .el-button {
  width: auto;
}
</style>